package com.lgs.demo.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.lgs.demo.R;
import com.lgs.demo.customview.NoScrollViewPager;
import com.lgs.demo.utils.LogUtil;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewActivity extends AppCompatActivity {

    @BindView(R.id.tabLayout)
    SmartTabLayout mTabLayout;
    @BindView(R.id.viewpager)
    public NoScrollViewPager mViewpager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        ButterKnife.bind(this);
        //standard、singleTop、singleTask、singleInstance
        LogUtil.i("onCreate");
        initView();
    }

    private void initView() {
        PagerInfo[] values = PagerInfo.values();
        for (int i = 0; i < values.length; i++) {
            values[i].setViewPager(mViewpager);
        }
        mViewpager.setAdapter(new TabAdapter(getSupportFragmentManager(), this));
        mTabLayout.setViewPager(mViewpager);
        mViewpager.addOnPageChangeListener(new MyOnPageChangeListener());
    }

    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        /**
         * 当页面滚动了的时候回调这个方法
         * @param position  当前页面的位置
         * @param positionOffset    滑动页面的百分比
         * @param positionOffsetPixels  在屏幕上滑动的像素
         */
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        /**
         * 当某个页面被选中了的时候回调
         * @param position  被选中页面的位置
         */
        @Override
        public void onPageSelected(int position) {

        }

        /**
         * 当页面滚动状态的变化的时候回调
         * @param state 静止——>滑动、滑动——>静止、静止——>拖拽
         */
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    private class TabAdapter extends FragmentPagerAdapter {

        private Context context;

        public TabAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public Fragment getItem(int position) {
            PagerInfo[] values = PagerInfo.values();
            PagerInfo value = values[position];
            Class clz = value.getClz();
            return Fragment.instantiate(context, clz.getName());
        }

        @Override
        public int getCount() {
            return PagerInfo.values().length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return PagerInfo.values()[position].title;
        }
    }

    public enum PagerInfo {
        MARKET("BTC", BTCPagerFragment.class),
        TRADING("UT", UTPagerFragment.class);

        private String title;
        private Class clz;
        private ViewPager viewPager;

        public String getTitle() {
            return title;
        }

        public Class getClz() {
            return clz;
        }

        PagerInfo(String title, Class clz) {
            this.title = title;
            this.clz = clz;
        }

        public ViewPager getViewPager() {
            return viewPager;
        }

        public void setViewPager(ViewPager viewPager) {
            this.viewPager = viewPager;
        }
    }


}
